package com.ruoyi.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.mapper.ZcSpecificationsMapper;
import com.ruoyi.sales.domain.ZcSpecifications;
import com.ruoyi.sales.service.IZcSpecificationsService;

/**
 * 产品规格Service业务层处理
 *
 * @author woo
 * @date 2024-04-02
 */
@Service
public class ZcSpecificationsServiceImpl extends ServiceImpl<ZcSpecificationsMapper, ZcSpecifications> implements IZcSpecificationsService {
    @Autowired
    private ZcSpecificationsMapper zcSpecificationsMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询产品规格
     *
     * @param specificationsId 产品规格主键
     * @return 产品规格
     */
    @Override
    public ZcSpecifications selectZcSpecificationsBySpecificationsId(Long specificationsId) {
        return zcSpecificationsMapper.selectZcSpecificationsBySpecificationsId(specificationsId);
    }

    /**
     * 查询产品规格列表
     *
     * @param zcSpecifications 产品规格
     * @return 产品规格
     */
    @Override
    public List<ZcSpecifications> selectZcSpecificationsList(ZcSpecifications zcSpecifications) {
        return zcSpecificationsMapper.selectZcSpecificationsList(zcSpecifications);
    }

    /**
     * 新增产品规格
     *
     * @param zcSpecifications 产品规格
     * @return 结果
     */
    @Override
    public int insertZcSpecifications(ZcSpecifications zcSpecifications) {
                zcSpecifications.setCreateTime(DateUtils.getNowDate());
        String wide = String.valueOf(zcSpecifications.getWide());
        String wideReal = sysDictDataService.selectDictLabel("zc_specifications", wide);
        String weight = String.valueOf(zcSpecifications.getWeight());
        String weightSb = weight.substring(0, 2);
        String result = wideReal + weightSb;
        int number = Integer.parseInt(result);
        zcSpecifications.setNumber((long) number);
        zcSpecifications.setDel((long) 0);
            return zcSpecificationsMapper.insertZcSpecifications(zcSpecifications);
    }

    /**
     * 修改产品规格
     *
     * @param zcSpecifications 产品规格
     * @return 结果
     */
    @Override
    public int updateZcSpecifications(ZcSpecifications zcSpecifications) {
                zcSpecifications.setUpdateTime(DateUtils.getNowDate());
        String wide = String.valueOf(zcSpecifications.getWide());
        String wideReal = sysDictDataService.selectDictLabel("zc_specifications", wide);
        String weight = String.valueOf(zcSpecifications.getWeight());
        String weightSb = weight.substring(0, 2);
        String result = wideReal + weightSb;
        int number = Integer.parseInt(result);
        zcSpecifications.setNumber((long) number);
        return zcSpecificationsMapper.updateZcSpecifications(zcSpecifications);
    }

    /**
     * 批量删除产品规格
     *
     * @param specificationsIds 需要删除的产品规格主键
     * @return 结果
     */
    @Override
    public int deleteZcSpecificationsBySpecificationsIds(Long[] specificationsIds) {
        return zcSpecificationsMapper.deleteZcSpecificationsBySpecificationsIds(specificationsIds);
    }

    /**
     * 删除产品规格信息
     *
     * @param specificationsId 产品规格主键
     * @return 结果
     */
    @Override
    public int deleteZcSpecificationsBySpecificationsId(Long specificationsId) {
        return zcSpecificationsMapper.deleteZcSpecificationsBySpecificationsId(specificationsId);
    }

    @Override
    public int updateSpecificationsStatus(ZcSpecifications zcSpecifications) {
        return zcSpecificationsMapper.updateSpecificationsStatus(zcSpecifications);
    }

    @Override
    public boolean checkSpecificationsUnique(ZcSpecifications zcSpecifications) {
        QueryWrapper<ZcSpecifications> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("status",0);
        List<ZcSpecifications> list = this.zcSpecificationsMapper.selectList(qw);
        for (ZcSpecifications specifications : list) {
            if (specifications.getWide().equals(zcSpecifications.getWide()) && specifications.getWeight().equals(zcSpecifications.getWeight())){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<ZcSpecifications> selectAll() {
        QueryWrapper<ZcSpecifications> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("status",0);
        List<ZcSpecifications> zcSpecifications = zcSpecificationsMapper.selectList(qw);
        return zcSpecifications;
    }

    public List<ZcSpecifications> selectWidthAndWeight() {
        QueryWrapper<ZcSpecifications> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("status",0);
        List<ZcSpecifications> zcSpecifications = zcSpecificationsMapper.selectList(qw);
        return zcSpecifications;
    }
}